/**
* Abstract: Object Animation Class and Functions
*
* Version: 1.0
*
* Disclaimer: IMPORTANT:  This Gamenova software is supplied to you by
* 4E Studios Srl("4E") in consideration of your agreement to the following terms,
* and your use, installation, modification or redistribution of this 4E software
* constitutes acceptance of these terms.  If you do not agree with these terms,
* please do not use, install, modify or redistribute this 4E software.
*
* In consideration of your agreement to abide by the following terms, and subject
* to these terms, 4E grants you a personal, non-exclusive license, under
* 4E's copyrights in this original 4E software (the "4E Software"), to
* use, reproduce, modify and redistribute the 4E Software, with or without
* modifications, in source and/or binary forms; provided that if you redistribute
* the 4E Software in its entirety and without modifications, you must retain
* this notice and the following text and disclaimers in all such redistributions
* of the 4E Software.
* Neither the name, trademarks, service marks or logos of 4E Studios Srl may be used
* to endorse or promote products derived from the 4E Software without specific
* prior written permission from 4E.  Except as expressly stated in this notice,
* no other rights or licenses, express or implied, are granted by 4E herein,
* including but not limited to any patent rights that may be infringed by your
* derivative works or by other works in which the 4E Software may be
* incorporated.
*
* The 4E Software is provided by 4E on an "AS IS" basis.  4E MAKES NO
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, REGARDING THE 4E SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
* COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL 4E BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
* DISTRIBUTION OF THE 4E SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
* CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
* 4E HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright (C) 2011 4E Studios Srl All Rights Reserved.
*/



/**
Linear Interpolation function
*/
function lerp(t){

	return t;

};





/**
	Property config class
	@constructor
*/
function GNAnimPropConfig(config){

	/**
		name of the property
	*/
	this.name = config.name == null ? null : config.name;

	/**
		min value
	*/
	this.min = config.min == null ? 0 : config.min;

	/**
		max value
	*/
	this.max = config.max == null ? Infinity : config.max;

	/**
		Set function
	*/
	this.setFunction = config.setFunction == null ? null : config.setFunction;

	/**
		Animatin ease function. Default linear interpolatio 
	*/
	this.easeFunction = config.easeFunction == null ? lerp : config.easeFunction;
}

/**
	Animator Class
	@constructor
*/
function GNAnimator(config){

	if(config == null || config.name == null)
	{
		return;
	}

	this.name = config.name;

	/**
		properties to animate
	*/
	this.properties = config.properties == null ? [] : config.properties;

	/**
		Object to animate
	*/
	this.target = config.target == null ? null : config.target;

	/**
		Animation length in milliseconds. Default 1 sec
	*/
	this.animationLength = config.animationLength == null ? 1000 : config.animationLength;

	/**
		Elapsed time
	*/
	this.elaspedTime = 0;

	/**
		reference to end animation event callback
	*/
	this.endAnimCallback = config.callback || function(){};

	/**
		Updates the interpolator state based on time
	*/
	this.code = function(dt){


		this.elaspedTime += dt;

		// animation end check
		if(this.elaspedTime >= this.animationLength)
		{
			GNCore.destroy(this.name);
			this.endAnimCallback();
		}

		var calct = this.elaspedTime/this.animationLength;

		var t = calct > 1 ? 1 : calct;

		for(var i=0; i<this.properties.length; i++){

			var propConfig = this.properties[i];

			var interpValue = propConfig.min + (propConfig.max - propConfig.min)*propConfig.easeFunction(t);

			if(propConfig.setFunction == null)
				this.target[propConfig.name] = interpValue;
			else
				this.target[propConfig.setFunction](interpValue);


		};


	};

	/**
		Adds itself to the task execution list
	*/
	this.updateInterval = config.updateInterval || 0;

	GNCore.list[this.name] =  this;
}
GNAnimator.prototype = new Task();


